package com.wiki.controller;


import com.wiki.service.EbookService;
import com.wiki.utils.commonRequest.ebook.EbookQueryReq;
import com.wiki.utils.commonRequest.ebook.EbookSaveReq;
import com.wiki.utils.commonResponse.CommonResp;
import com.wiki.utils.commonResponse.EbookQueryResp;
import com.wiki.utils.commonResponse.PageResp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * <p>
 * 电子书 前端控制器
 * </p>
 *
 * @author 偷闰土的猹
 * @since 2022-01-04
 */
@RestController
@RequestMapping("/ebook")
public class EbookController {
    @Autowired
    EbookService ebookService;


    @GetMapping("/list")
    public CommonResp list(@Valid EbookQueryReq req) {

        CommonResp<PageResp<EbookQueryResp>> listCommonResp = new CommonResp<>();
        PageResp<EbookQueryResp> name = ebookService.listAll(req);
        listCommonResp.setContent(name);
        return listCommonResp;
    }

    @GetMapping("/list_by_name")
    public CommonResp list_by_name(@Valid EbookQueryReq req) {
        CommonResp<PageResp<EbookQueryResp>> listCommonResp = new CommonResp<>();
        PageResp<EbookQueryResp> resp = ebookService.queryLike(req);
        listCommonResp.setContent(resp);
        return listCommonResp;
    }

    @PostMapping("/save")
    public CommonResp save(@Valid @RequestBody EbookSaveReq req) {
        CommonResp resp = new CommonResp();
        ebookService.save(req);
        return resp;
    }

    @DeleteMapping("/delete/{id}")
    public CommonResp delete(@PathVariable Long id) {
        CommonResp resp = new CommonResp();
        ebookService.delete(id);

        return resp;
    }

}
